<?php
namespace app\farm\model;

use app\BaseModel;
use think\facade\Db;

class Seed extends BaseModel
{
    //定义表名
    protected $name = 'lbfarm_seed';


    protected $append = [

        'source_id'

    ];


    /**
     * @author chenniang
     * @DataTime: 2022-02-12 14:18
     * @功能说明:种子
     */
    public function getSourceIdAttr($value,$data){

        if(!empty($data['id'])){

           $source_model = new LandSourceText();

            $dis = [

                'a.obj_id' => $data['id'],

                'type'   => 1
            ];

           $list = $source_model->alias('a')
                    ->join('lbfarm_source b','b.id = a.source_id')
                    ->where($dis)
                    ->field('a.*,b.title')
                    ->select()
                    ->toArray();

           return $list;

        }

    }


    /**
     * @author chenniang
     * @DataTime: 2022-01-07 10:08
     * @功能说明:月份
     */
    public function getMonthAttr($value,$data){

        if(!empty($data['id'])){

            $month_model = new SeedMonth();

            $list = $month_model->where(['seed_id'=>$data['id']])->column('month');

            return array_values($list);

        }

    }


    /**
     * @author chenniang
     * @DataTime: 2021-12-30 16:54
     * @功能说明:
     */
    public function getImgsAttr($value,$data){

        if(!empty($value)){

            return explode(',',$value);

        }


    }

    /**
     * @author chenniang
     * @DataTime: 2020-09-29 11:04
     * @功能说明:添加
     */
    public function dataAdd($data){

        $data['imgs'] = !empty($data['imgs'])?implode(',',$data['imgs']):'';

        $data['create_time'] = time();

        if(isset($data['month'])){

            $month = $data['month'];

            unset($data['month']);
        }

        if(isset($data['source_id'])){

            $source_id = $data['source_id'];

            unset($data['source_id']);
        }

        $res = $this->insert($data);

        $id  = $this->getLastInsID();

        if(isset($month)){

            $this->updateSome($month,$id,$data['uniacid']);
        }

        return $id;

    }


    /**
     * @author chenniang
     * @DataTime: 2022-01-07 10:03
     * @功能说明:添加种子月份
     */
    public function updateSome($data,$id,$uniacid,$seed=[]){

        $month_model = new SeedMonth();

        $month_model->where(['seed_id'=>$id])->delete();

        if(!empty($data)){

            foreach ($data as $k=>$v){

                $season = ceil($v/3);

                $insert[$k]=[

                    'uniacid' => $uniacid,

                    'seed_id' => $id,

                    'month'   => $v,

                    'season'  => $season
                ];

            }

            $month_model->saveAll($insert);
        }

        $soruce_model = new LandSourceText();

        $soruce_model->where(['obj_id'=>$id,'type'=>1])->delete();

        if(!empty($seed)){

            foreach ($seed as $key=>$value){

                $insertdata[$key] = [

                    'uniacid'   => $uniacid,

                    'obj_id'    => $id,

                    'source_id' => $value,

                    'type'      => 1
                ];

            }

            $soruce_model->saveAll($insertdata);

        }

        return true;

    }



    /**
     * @author chenniang
     * @DataTime: 2020-09-29 11:05
     * @功能说明:编辑
     */
    public function dataUpdate($dis,$data){

        if(isset($data['month'])){

            $month = $data['month'];

            unset($data['month']);
        }

        if(isset($data['source_id'])){

            $source_id = $data['source_id'];

            unset($data['source_id']);
        }

        if(!empty($data['imgs'])){

            $data['imgs'] = implode(',',$data['imgs']);
        }

        $res = $this->where($dis)->update($data);

        if(isset($month)){

            $this->updateSome($month,$dis['id'],$data['uniacid'],[]);
        }

        return $res;

    }


    /**
     * @author chenniang
     * @DataTime: 2020-09-29 11:06
     * @功能说明:列表
     */
    public function dataList($dis,$page){

        $data = $this->where($dis)->order('top desc,id desc')->paginate($page)->toArray();

        return $data;

    }


    /**
     * @author chenniang
     * @DataTime: 2020-09-29 11:43
     * @功能说明:
     */
    public function dataInfo($dis){

        $data = $this->where($dis)->find();

        return !empty($data)?$data->toArray():[];

    }


    /**
     * @author chenniang
     * @DataTime: 2022-01-07 10:31
     * @功能说明:列表
     */
    public function indexDataList($dis,$sort,$page = 10){

        switch ($sort){

            case 1:

                $order = 'a.id desc';

                break;

            default:

                $order = 'a.sale_num desc,a.id desc';

                break;

        }

        $data = $this->alias('a')
                ->join('lbfarm_seed_month b','a.id = b.seed_id','left')
                ->where($dis)
                ->field('a.*')
                ->group('a.id')
                ->order($order)
                ->paginate($page)
                ->toArray();

        return $data;

    }


    /**
     * @author chenniang
     * @DataTime: 2022-02-16 13:39
     * @功能说明:修改库存
     */
    public function updateSaleNum($seed_id,$num,$type=1){
        //加销量
        if($type==1){

            $res = $this->where(['id'=>$seed_id])->update(['sale_num'=>Db::Raw("sale_num+$num")]);

        }else{

            $res = $this->where(['id'=>$seed_id])->update(['sale_num'=>Db::Raw("sale_num-$num")]);

        }

        return $res;
    }










}